import BasicLayout from '../views/Layout/BasicLayout'
import EmptyRouterView from '../views/Layout/EmptyRouterView'
const stadicRoutes = [
    {
        path: '/user',
        name: 'user',
        redirect: '/user/login',
        component: () => import(/* webpcakChunkName: "User" */ '../views/User/index.vue'),
        children: [
            {
                path: '/user/login',
                name: 'Login',
                component: () =>
                    import(/* webpcakChunkName: "Login" */ '../views/User/Login.vue')
            }
        ]
    },
    {
        path: '/404',
        component: () =>
            import(/* webpackChunkName: "notFound" */ '../views/Layout/NotFound.vue')
    }
    // // 未匹配到的路由走404
    // {
    //   path: '*',
    //   redirect: '/404',
    //   meta: {
    //     hideInMenu: true
    //   }
    // }
]

const asyncRoutes = [
    {
        path: '/',
        name: 'index',
        component: BasicLayout,
        redirect: '/welcome',
        meta: {
            hideInMenu: false
        },
        children: [
            {
                path: '/welcome',
                name: 'Welcome',
                component: () =>
                    import(/* webpackChunkName: "Welcome" */ '../views/Home.vue'),
                meta: {
                    permission: ['welcome'],
                    menuItem: true,
                    name: '首页',
                    icon: 'home'
                }
            },
            {
                path: '/list',
                name: 'List',
                redirect: '/list/goods-list',
                component: () =>
                    import(/* webpackChunkName: "List" */ '../views/List/index.vue'),
                meta: {
                    permission: ['list'],
                    name: '列表',
                    menuItem: true
                },
                children: [
                    // 子路由
                    {
                        path: '/list/goods-list',
                        name: 'GoodsList',
                        component: () =>
                            import(
                                /* webpackChunkName: "GoodsList" */ '../views/List/GoodsList.vue'
                            ),
                        meta: {
                            permission: ['list'],
                            menuItem: true,
                            name: '列表1'
                        }
                    },
                    {
                        path: '/list/drugs-list',
                        name: 'DrugsList',
                        component: () =>
                            import(
                                /* webpackChunkName: "GoodsList" */ '../views/List/DrugsList.vue'
                            ),
                        meta: {
                            permission: ['list', 'DrugsList'],
                            menuItem: true,
                            name: '列表2'
                        }
                    },
                    {
                        path: '/list/fruites-list',
                        name: 'FruitesList',
                        component: () =>
                            import(
                                /* webpackChunkName: "GoodsList" */ '../views/List/FruitesList.vue'
                            ),
                        meta: {
                            permission: ['list', 'fruites-list'],
                            menuItem: true,
                            name: '列表3'
                        }
                    }
                ]
            },
            {
                path: '/virtual-machine-manage',
                name: 'VirtualMachineManage',
                redirect: '/virtual-machine-manage/virtual-machine-list',
                component: EmptyRouterView,
                meta: {
                    permission: ['enable'],
                    name: '虚拟机管理',
                    menuItem: true,
                    icon: 'home'
                },
                children: [
                    // 虚拟机列表
                    {
                        path: '/virtual-machine-manage/virtual-machine-list',
                        name: 'VirtualMachineList',
                        component: () =>
                            import(
                                /* webpackChunkName: "VirtualMachineList" */ '../views/VirtualMachineManage/VirtualMachineList'
                            ),
                        meta: {
                            permission: ['enable'],
                            menuItem: true,
                            name: '虚拟机列表'
                        }
                    },
                    // 虚拟机详情
                    {
                        path: '/virtual-machine-manage/virtual-machine-detail/?:id',
                        name: 'VirtualMachineDetail',
                        component: () =>
                            import(
                                /* webpackChunkName: "VirtualMachineDetail" */ '../views/VirtualMachineManage/VirtualMachine'
                            ),
                        meta: {
                            permission: ['enable'],
                            menuItem: false,
                            name: '虚拟机详情'
                        }
                    },
                    // 模板管理
                    {
                        path: '/virtual-machine-manage/template-manage',
                        name: 'TemplateManage',
                        component: () =>
                            import(
                                /* webpackChunkName: "TemplateManage" */ '../views/VirtualMachineManage/TemplateManage'
                            ),
                        meta: {
                            permission: ['enable'],
                            menuItem: true,
                            name: '模板管理'
                        }
                    }
                ]
            },
            {
                path: '/member',
                name: 'Member',
                redirect: '/member/member-manage',
                component: EmptyRouterView,
                meta: {
                    name: '用户管理',
                    permission: ['enable'],
                    menuItem: true,
                    icon: 'admin'
                },
                children: [
                    {
                        path: '/member/member-manage',
                        name: 'MemberManage',
                        component: () => import(/* webpackChunkName: "MemberManage" */'../views/MemberManage/UserManage'),
                        meta: {
                            name: '用户管理',
                            menuItem: true,
                            permission: ['enable']
                        }
                    },
                    {
                        path: '/member/permission-manage',
                        name: 'PermissionManage',
                        component: () => import(/* webpackChunkName: "PermissionManage" */'../views/MemberManage/PermissionManage'),
                        meta: {
                            name: '权限管理',
                            menuItem: true,
                            permission: ['enable']
                        }
                    }
                ]
            },
            {
                path: '/net-manage',
                name: 'NetManage',
                redirect: '/net-manage/net-list',
                component: EmptyRouterView,
                meta: {
                    name: '网络管理',
                    permission: ['enable'],
                    menuItem: true,
                    icon: 'home'
                },
                children: [
                    {
                        path: '/net-manage/net-list',
                        name: 'NetList',
                        component: () => import(/* webpackChunkName: "NetList" */'../views/NetManage/NetList'),
                        meta: {
                            name: '网络列表',
                            menuItem: true,
                            permission: ['enable']
                        }
                    },
                    {
                        path: '/net-manage/net-topo',
                        name: 'NetTopo',
                        component: () => import(/* webpackChunkName: "NetTopo" */'../views/NetManage/NetTopo'),
                        meta: {
                            name: '网络拓扑',
                            menuItem: true,
                            permission: ['enable']
                        }
                    }
                ]
            },
            {
                path: '/system-manage',
                name: 'SystemManage',
                redirect: '/system-manage/service-device',
                component: EmptyRouterView,
                meta: {
                    name: '系统管理',
                    permission: ['enable'],
                    menuItem: true,
                    icon: 'edit'
                },
                children: [
                    {
                        path: '/system-manage/service-device',
                        name: 'ServiceDevice',
                        component: () => import(/* webpackChunkName: "ServiceDevice" */'../views/SysTemManage/ServiceManage'),
                        meta: {
                            name: '服务器设备',
                            menuItem: true,
                            permission: ['enable']
                        }
                    },
                    // 详情
                    {
                        path: '/system-manage/service-device/computed-detail/:id?',
                        name: 'ComputedDetail',
                        meta: {
                            name: '计算详情',
                            menuItem: false,
                            permission: ['enable']
                        },
                        component: () => import(/* webpackChunkName: "ComputedDetail" */'../views/SysTemManage/DeviceDetails/ComputedDetail')
                    },
                    {
                        path: '/system-manage/service-device/memory-detail/:id?',
                        name: 'MemoryDetail',
                        meta: {
                            name: '存储详情',
                            menuItem: false,
                            permission: ['enable']
                        },
                        component: () => import(/* webpackChunkName: "MemoryDetail" */'../views/SysTemManage/DeviceDetails/MemoryDetail')
                    },
                    // 详情
                    {
                        path: '/system-manage/terminal-device',
                        name: 'TerminalDevice',
                        component: () => import(/* webpackChunkName: "TerminalDevice" */'../views/SysTemManage/TerminalManage'),
                        meta: {
                            name: '终端设备',
                            menuItem: true,
                            permission: ['enable']
                        }
                    },
                    // 告警配置
                    {
                        path: '/system-manage/alarm-setting',
                        name: 'AlarmSetting',
                        component: () => import(/* webpackChunkName: "AlarmSetting" */'../views/SysTemManage/AlarmSetting'),
                        meta: {
                            name: '告警配置',
                            menuItem: true,
                            permission: ['enable']
                        }
                    },
                    {
                        path: '/system-manage/log-manage',
                        name: 'LogManage',
                        component: () => import(/* webpackChunkName: "LogManage" */'../views/SysTemManage/LogManage'),
                        meta: {
                            name: '日志管理',
                            menuItem: true,
                            permission: ['enable']
                        }
                    },
                    {
                        path: '/system-manage/system-maintenance',
                        name: 'SystemMaintenance',
                        component: () => import(/* webpackChunkName: "SystemMaintenance" */'../views/SysTemManage/SystemMaintenance'),
                        meta: {
                            name: '系统维护',
                            menuItem: true,
                            permission: ['enable']
                        }
                    }
                ]
            },
            {
                path: '/topo',
                name: 'Topo',
                component: () =>
                    import(/* webpackChunkName: "Topo" */ '../views/Topo/Topo.vue'),
                meta: {
                    permission: ['topo']
                }
            },
            {
                path: '/charts',
                name: 'Charts',
                component: () =>
                    import(/* webpackChunkName: "Charts" */ '../views/Charts/Chart.vue'),
                meta: {
                    permission: ['charts']
                }
            },
            {
                path: '/other',
                name: 'Other',
                component: () => import(/* webpackChunkName: "Other" */'../views/Other/Other.vue'),
                meta: {
                    permission: ['other']
                }
            },
            {
                path: '/problem',
                name: 'Problem',
                component: () => import(/* webpackChunkName: "Problem" */'../views/Problem/Problem.vue'),
                meta: {
                    permission: ['other']
                }
            }
        ]
    },
    {
        path: '/404',
        name: 'NotFound',
        component: () =>
            import(/* webpackChunkName: "NotFound" */ '../views/Layout/NotFound.vue'),
        meta: {
            hideInMenu: true
        }
    },
    // 未匹配到的路由走404
    {
        path: '*',
        redirect: '/404',
        meta: {
            hideInMenu: true
        }
    }
]

// 判断当前route是否有 权限
function hasPermission (permission, route) {
    if (route.meta && route.meta.permission) {
        return route.meta.permission.every((item) => {
            return permission.includes(item)
        })
    }
    return true
}

const filterRoutes = (menus, routes) => {
    return routes.filter((route) => {
        if (hasPermission(menus, route)) {
            if (route.children && route.children.length) {
                route.children = filterRoutes(menus, route.children)
            }
            return true
        }
        return false
    })
}

export { stadicRoutes, asyncRoutes, filterRoutes }
